import java.awt.Color;
import java.awt.Font;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Random;

import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.plugin.PlugIn;
import ij.process.ImageProcessor;

// On fournit deux images (meme taille), on fabrique un masque et les deux images transformees

public class cryptoDraw extends ImagePlus implements PlugIn{
	
	//private ImageProcessor primeIP; 
	private int X=500,Y=500; // image size
	
	private ImagePlus maskAlpha;  
	private ImageProcessor maskAlphaIP;  

	
	private Random generator=new Random(1214); 
	
	public void run(String arg) {
		System.out.println("go");
		
		// Construction du masque aleatoire
		maskAlpha = NewImage.createRGBImage ("blackmask",2*X,2*Y, 1, NewImage.FILL_WHITE);
		maskAlphaIP = maskAlpha.getProcessor();
 		int memory[][]=new int[2*X][2*Y]; 
 		int compteur=0; 
 		for(int i=0;i<2*X;i++)
 			for(int j=0;j<2*Y;j+=2){
 				int pix=generator.nextInt(2);
 					memory[i][j]=pix;
 					memory[i][j+1]=1-pix; 
 					maskAlphaIP.putPixel(i, j, pix*0xFFFFFF); 
 					maskAlphaIP.putPixel(i, j+1, (1-pix)*0xFFFFFF); 
 				}	
 			maskAlpha.show();
 
 			System.out.println("end !");
	
 			ImagePlus imageCachee; 
 			imageCachee=IJ.openImage("C:/Users/decomite/Pictures/crypto/jp/oiseau.jpg"); 
 			imageCachee.setTitle("oiseau");
 			ImageProcessor icP=imageCachee.getProcessor(); 
 			imageCachee.show();
 			
 			
	ImagePlus image1; 
	image1=IJ.openImage("C:/Users/decomite/Pictures/crypto/jp/elephant.jpg"); 
	image1.setTitle("Image 1");
	ImageProcessor i1P=image1.getProcessor(); 
	image1.show();
	
	
	
	
	ImagePlus image1Transformed=NewImage.createRGBImage ("Image1Transformed",2*X,2*Y, 1, NewImage.FILL_WHITE);
	ImageProcessor i1TProcessor=image1Transformed.getProcessor(); 
	for(int i=0;i<X;i++)
		for(int j=0;j<Y;j++){
			int value=i1P.getPixel(i,j)& 0x0000FF;
		
			if(value>128){
				i1TProcessor.putPixel(2*i,2*j,memory[2*i][2*j]*0xFFFFFF);
				i1TProcessor.putPixel(2*i,2*j+1,memory[2*i][2*j+1]*0xFFFFFF);
				
			}
			else{
				i1TProcessor.putPixel(2*i,2*j,0);
				i1TProcessor.putPixel(2*i,2*j+1,0);
			}
			i1TProcessor.putPixel(2*i+1,2*j,memory[2*i+1][2*j]*0xFFFFFF);
			i1TProcessor.putPixel(2*i+1,2*j+1,memory[2*i+1][2*j+1]*0xFFFFFF);
		}
				
	image1Transformed.show();
    
    ImagePlus image2; 
	image2=IJ.openImage("C:/Users/decomite/Pictures/crypto/jp/cheval.jpg"); 
	ImageProcessor i2P=image2.getProcessor(); 
	image2.setTitle("cheval");
	
	
   image2.show();
   ImagePlus image2Transformed=NewImage.createRGBImage ("Image2Transformed",2*X,2*Y, 1, NewImage.FILL_WHITE);
	ImageProcessor i2TProcessor=image2Transformed.getProcessor(); 
	for(int i=0;i<X;i++)
	for(int j=0;j<Y;j++){
		int  value=i2P.getPixel(i,j)&0x0000FF;
		System.out.println(value); 
		if(value>128){
			i2TProcessor.putPixel(2*i,2*j,(1-memory[2*i][2*j])*0xFFFFFF);
			i2TProcessor.putPixel(2*i,2*j+1,(1-memory[2*i][2*j+1])*0xFFFFFF);
		}
		else{
			i2TProcessor.putPixel(2*i,2*j,0);
			i2TProcessor.putPixel(2*i,2*j+1,0);
		}
		if(icP.get(i,j)>128){
		i2TProcessor.putPixel(2*i+1,2*j,memory[2*i+1][2*j]*0xFFFFFF);
		i2TProcessor.putPixel(2*i+1,2*j+1,memory[2*i+1][2*j+1]*0xFFFFFF);
		}
		else
		{
			i2TProcessor.putPixel(2*i+1,2*j,(1-memory[2*i+1][2*j])*0xFFFFFF);
			i2TProcessor.putPixel(2*i+1,2*j+1,(1-memory[2*i+1][2*j+1])*0xFFFFFF);
		}
		
	}
			
image2Transformed.show();
	
    	show();
	

}
}
